// pages/addclient/addclient.js
import {addClient,getClientInfo,editClient,formatDate} from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {    
    show: false,
    showDate:false,
    showPro:false,
    showMd:false,
    minHour:8,
    maxHour:18,
    minDate: new Date().getTime(),
    columns: [],
    projects: [],
    shops: [],
    nameMsg:'',
    phoneMsg:'',
    countMsg:'',
    autosize:{
      maxHeight: 125, minHeight: 60
    },
    condition:{},
    name:'',
    phone:'',
    type:"",
    count:'',
    remark:'',
    currentDate:'',
    date:'',
    mendian:'',
    project:'',
    currentId:'',
    template_ids:[]
  },
  initField(e){
    var fieldName=e.currentTarget.id
    switch (fieldName){
      case 'nameMsg':
        this.setData({
          nameMsg: '',
        })
        break;
      case 'phoneMsg':
        this.setData({
          phoneMsg: '',
        })
        break;
      case 'countMsg':
        this.setData({
          countMsg: '',
        })
        break;
    }
  },
  onDisplay() {
    this.setData({      
      show: true 
    });
  },
  onClose() {
    this.setData({ show: false });
  },

  onChange(event) {
    this.setData({
      show: false,
      type: event.detail.value,
    });
  },


  onDisplayDate() {
    this.setData({minDate: new Date().getTime(), showDate: true });
  },
  onCloseDate() {
    this.setData({ showDate: false });
  },
  onConfirm(event) {
    this.setData({
      showDate: false,
      date: formatDate(event.detail),
    });
  },

  onDisplayPro() {
    this.setData({ showPro: true });
  },
  onClosePro() {
    this.setData({ showPro: false });
  },

  onChangePro(event) {
    this.setData({
      showPro: false,
      project: event.detail.value,
    });
  },

  onDisplayMd() {
    this.setData({ showMd: true });
  },
  onCloseMd() {
    this.setData({ showMd: false });
  },

  onChangeMd(event) {
    this.setData({
      showMd: false,
      mendian: event.detail.value,
    });
  },
  isMobile: function (value) {
    return /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
  },
  findId(name,data){
    let id=''
    data.forEach(item => {
      if(item.label==name){
        id= item.value;
      }
    });
    return id;
  },
  formSubmit(e){    
    var flag=false
    if (!e.detail.value.name) {
      this.setData({
        nameMsg: '请输入姓名',
      })
      flag=true
    }
    if (!e.detail.value.phone){
      this.setData({
        phoneMsg: '请输入手机号',
      })
      flag=true
    }else if (!this.isMobile(e.detail.value.phone)){      
      this.setData({
        phoneMsg: '手机号格式错误',
      })
      flag=true
    }
    if (!e.detail.value.count) {
      this.setData({
        countMsg: '请输入折扣',
      })
      flag=true
    }else if(e.detail.value.count<60||e.detail.value.count>100){
      this.setData({
        countMsg: '建议7折以上',
      })
      flag=true
    }
    if(this.data.date){
      if(!this.data.project){
        wx.showToast({
          title: '请选择项目',
          icon: 'error',
          duration: 2000
        })
      }else if(!this.data.mendian){
        wx.showToast({
          title: '请选择门店',
          icon: 'error',
          duration: 2000
        })
      }
    }
    if(flag){
      return;
    }
    let params={
      name:e.detail.value.name,
      mobile:e.detail.value.phone,
      type_id:this.findId(this.data.type,this.data.condition.patient_type),
      order_time:this.data.date,
      treatment_project_id:this.findId(this.data.project,this.data.condition.treatment_project),
      shop_id:this.findId(this.data.mendian,this.data.condition.shop),
      discount_rate:e.detail.value.count,
      remark:e.detail.value.remark
    }
    if(this.data.currentId>0){      
      params={
        id:this.data.currentId,
        name:e.detail.value.name,
        mobile:e.detail.value.phone,
        type_id:this.findId(this.data.type,this.data.condition.patient_type),
        discount_rate:e.detail.value.count,
        remark:e.detail.value.remark
      }
      editClient(params).then(res=>{   
        let pages=getCurrentPages()
        let beforePage=pages[pages.length-2]
        let page2=pages[pages.length-3]  
        beforePage.getData() 
        page2.refresh()  
        wx.navigateBack({
          data:1
        })        
      })
    }else{      
      let that=this
      wx.requestSubscribeMessage({
        tmplIds: that.data.template_ids,
        success(res1) {
          addClient(params).then(res=>{
            wx.reLaunch({
              url: '/pages/client/client'
            })
         })
       },
       fail(err){
         console.log(err)
          addClient(params).then(res=>{
              wx.reLaunch({
                url: '/pages/client/client'
              })
          })
       }
      })
    } 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  changeData(data){
    let arr=[]
    data.forEach(item => {
      arr.push(item.label)
    });
    return arr;
  },
  onLoad(options) {
    this.setData({
      currentId:options.id?options.id:0
    })
    getClientInfo({id:this.data.currentId}).then(res=>{
      this.setData({
        name:res.data.name,
        phone:res.data.mobile,
        type:res.data.type_name,
        count:res.data.discount_rate,
        remark:res.data.remark,
        condition:res.data.condition,
        projects:this.changeData(res.data.condition.treatment_project),
        shops:this.changeData(res.data.condition.shop),
        columns:this.changeData(res.data.condition.patient_type),
        template_ids:res.data.template_ids?res.data.template_ids.filter((item)=>{
          return item!=''
        }):[]
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})